<template>
  <div style="padding: 10px">
    <!--功能区域-->
    <div style="margin: 10px 0">
      <el-button type="primary" @click="add">新增</el-button>
    </div>

    <!--搜索区域-->
    <div style="margin: 10px 0">
      <el-input v-model="search" placeholder="请输入关键字" style="width: 20%" clearable></el-input>
      <el-button type="primary" style="margin-left: 5px" @click="load">查询</el-button>
    </div>

    <!--数据区域-->
    <el-table :data="tableData" bo rder stripe style="width: 100%; margin: 10px 0">
      <el-table-column prop="id" label="ID" sortable=""></el-table-column>
      <el-table-column prop="title" label="标题"></el-table-column>
      <el-table-column prop="author" label="作者"></el-table-column>
      <el-table-column prop="time" label="发表时间"></el-table-column>
      <el-table-column fixed="right" label="操作" width="300px">
        <template #default="scope">
          <el-button size="mini" @click="showDetail(scope.row)">详情</el-button>
          <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-popconfirm title="您确定要删除吗?" @confirm="handleDelete(scope.row.id)">
            <template #reference>
              <el-button size="mini" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!--分页部分-->
    <div style="margin: 10px 0">
      <el-pagination
          v-model:currentPage="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[5, 10, 20]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange">
      </el-pagination>
    </div>

    <!--增加或编辑时候的弹窗-->
    <el-dialog title="提示" v-model="dialogVisible" width="50%">
      <el-form :model="form" label-width="120px">
        <el-form-item label="标题">
          <el-input v-model="form.title" style="width: 50% "></el-input>
        </el-form-item>
        <div id="div1">

        </div>
      </el-form>
      <template #footer>
          <span class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="save">确 定</el-button>
          </span>
      </template>
    </el-dialog>

    <!--展示详情-->
    <el-dialog title="正文" v-model="vis" width="50%">
      <el-card>
        <div v-html="detail.content" style="min-height: 200px"></div>
      </el-card>
    </el-dialog>

  </div>

</template>

<script>


import request from "@/utils/request";
import E from 'wangeditor';

let editor;

export default {
  //导出名
  name: 'News',
  components: {

  },
  //页面中的变量
  data() {
    return {
      form: {},
      dialogVisible: false,
      search: '',
      currentPage: 1,
      pageSize: 10,
      total: 0,
      tableData: [],
      vis: false,
      detail: {}
    }
  },
  //vue加载时的方法
  created() {
    this.load();
  },
  // //vue加载完后的方法
  // mounted(){
  //
  // },
  methods: {
    load() {
      this.loading = true;
      request.get("/news", {
        params: {
          pageNum: this.currentPage,
          pageSize: this.pageSize,
          search: this.search,
        }
      }).then(res => {
        this.loading = false;
        this.tableData = res.data.records;
        this.total = res.data.total;
      })
    },
    add() {
      this.dialogVisible = true;
      this.form = {};
      //$nextTick(() => {}) 给内部元素缓冲加载的时间，例如增加方法里要获取form表单中的div元素，但div可能尚未加载过
      this.$nextTick(() => {
        //关联增加new时候的弹窗div
        editor = new E('#div1');
        editor.config.uploadImgServer = "http://localhost:9090/files/editor/upload";
        //设置上传参数名称，与后端接口参数名相同
        editor.config.uploadFileName = "file";
        editor.create();
      });
    },
    save() {
      this.form.content = editor.txt.html();
      if (this.form.id) {  // 更新
        request.put("/news", this.form).then(res => {
          console.log(res);
          if (res.code === '0') {
            this.$message({
              type: "success",
              message: "更新成功"
            })
          } else {
            this.$message({
              type: "error",
              message: res.msg
            })
          }
          this.load(); // 刷新表格的数据
          this.dialogVisible = false  // 关闭弹窗
        })
      }  else {  // 新增
        let userStr = sessionStorage.getItem("user") || "{}";
        let user =JSON.parse(userStr);
        //给表单中的作者填值
        this.form.author = user.nickname;
        request.post("/news", this.form).then(res => {
          console.log(res);
          if (res.code === '0') {
            this.$message({
              type: "success",
              message: "新增成功"
            })
          } else {
            this.$message({
              type: "error",
              message: res.msg
            })
          }
          this.load(); // 刷新表格的数据
          this.dialogVisible = false  // 关闭弹窗
        })
      }
    },
    showDetail(row){
      this.detail = row;
      this.vis = true;
    },
    filesUploadSuccess(res){
      console.log(res);
      this.form.cover = res.data;
    },
    handleEdit(row) {
      this.form = JSON.parse(JSON.stringify(row));
      this.dialogVisible = true;
      this.$nextTick(() => {
        //关联增加new时候的弹窗div
        editor = new E('#div1');
        editor.config.uploadImgServer = "http://localhost:9090/files/editor/upload";
        //设置上传参数名称，与后端接口参数名相同
        editor.config.uploadFileName = "file";
        editor.create();
      });
    },
    handleDelete(id) {
      console.log(id);
      request.delete("/news/" + id).then(res => {
        if (res.code === '0') {
          this.$message({
            type: "success",
            message: "删除成功"
          })
        } else {
          this.$message({
            type: "error",
            message: res.msg
          })
        }
        this.load();  // 删除之后重新加载表格的数据
      })
    },
    handleSizeChange(){ //改变当前每页个数
      this.load();
    },
    handleCurrentChange(){  //改变当前所在页码
      this.load();
    },
  }
}
</script>
